package com.stationbuilding.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

public class PreferenceHelper {

    private SharedPreferences mSharedPreferences;

    private PreferenceHelper(Context context) {
        this.mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

    private static PreferenceHelper mPreferenceHelper;

    public static PreferenceHelper getInstance(Context context) {
        if (null == mPreferenceHelper) {
            mPreferenceHelper = new PreferenceHelper(context);
        }
        return mPreferenceHelper;
    }

    /**
     * token存取
     * @param token
     */
    public void saveToken(String token) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("token", token);
        editor.commit();
    }

    public String getToken() {
        String token = mSharedPreferences.getString("token", null);
        if (TextUtils.isEmpty(token)) {
            return null;
        } else {
            return token;
        }
    }

    /**
     * 站房Id存取
     * @param projectId
     */
    public void saveProjectId(String projectId) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("projectId", projectId);
        editor.commit();
    }

    public String getProjectId() {
        String projectId = mSharedPreferences.getString("projectId", null);
        if (TextUtils.isEmpty(projectId)) {
            return null;
        } else {
            return projectId;
        }
    }

    /**
     * 用户id存取
     * @param userId
     */
    public void saveUserId(String userId) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("userId", userId);
        editor.commit();
    }

    public String getUserId() {
        String userId = mSharedPreferences.getString("userId", null);
        if (TextUtils.isEmpty(userId)) {
            return null;
        } else {
            return userId;
        }
    }

    /**
     * 用户名存取
     * @param userName
     */
    public void saveUserName(String userName) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("userName", userName);
        editor.commit();
    }

    public String getUserName() {
        String userName = mSharedPreferences.getString("userName", null);
        if (TextUtils.isEmpty(userName)) {
            return null;
        } else {
            return userName;
        }
    }

    /**
     * 用户角色存取
     * @param roleIdList
     */
    public void saveRoleIdList(String roleIdList) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("roleIdList", roleIdList);
        editor.commit();
    }

    public String getRoleIdList() {
        String roleIdList = mSharedPreferences.getString("roleIdList", null);
        if (TextUtils.isEmpty(roleIdList)) {
            return null;
        } else {
            return roleIdList;
        }
    }

    /**
     * email存取
     * @param email
     */
    public void saveEmail(String email) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("email", email);
        editor.commit();
    }

    public String getEmail() {
        String email = mSharedPreferences.getString("email", null);
        if (TextUtils.isEmpty(email)) {
            return null;
        } else {
            return email;
        }
    }

    /**
     * 树数据存取
     * @param treeData
     */
    public void saveTreeData(String treeData) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("treeData", treeData);
        editor.commit();
    }

    public String getTreeData() {
        String treeData = mSharedPreferences.getString("treeData", null);
        if (TextUtils.isEmpty(treeData)) {
            return null;
        } else {
            return treeData;
        }
    }

    /**
     * 唯一标识存取
     * @param randoms
     */
    public void saveRandoms(String randoms) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("randoms", randoms);
        editor.commit();
    }

    public String getRandoms() {
        String randoms = mSharedPreferences.getString("randoms", null);
        if (TextUtils.isEmpty(randoms)) {
            return null;
        } else {
            return randoms;
        }
    }

    public void setAlarm(boolean isAlarm) {
        Editor editor = mSharedPreferences.edit();
        editor.putBoolean("alarm", isAlarm);
        editor.commit();
    }


    public boolean getAlarm() {
        return mSharedPreferences.getBoolean("alarm", false);
    }


    public void saveAccount(String account) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("account", account);
        editor.commit();
    }

    public boolean isAccountExist() {
        return !(null == getAccount());
    }

    public String getAccount() {
        String account = mSharedPreferences.getString("account", null);
        if (TextUtils.isEmpty(account)) {
            return null;
        } else {
            return account;
        }
    }

    public void savePassword(String password) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("password", password);
        editor.commit();
    }

    public String getPassword() {
        String password = mSharedPreferences.getString("password", null);
        if (TextUtils.isEmpty(password)) {
            return null;
        } else {
            return password;
        }
    }


    public void cleanPassword() {
        Editor editor = mSharedPreferences.edit();
        editor.remove("password");
        editor.commit();
    }

    public void cleanAccount() {
        Editor editor = mSharedPreferences.edit();
        editor.remove("username");
        editor.remove("password");
        editor.commit();
    }
    /**
     * 提醒时间存取
     * @param remindTime
     */
    public void saveRemindTime(String remindTime) {
        Editor editor = mSharedPreferences.edit();
        editor.putString("remindTime", remindTime);
        editor.commit();
    }

    public String getRemindTime() {
        String remindTime = mSharedPreferences.getString("remindTime", null);
        if (TextUtils.isEmpty(remindTime)) {
            return null;
        } else {
            return remindTime;
        }
    }

    public void clearSP(){
        Editor editor = mSharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
}